package cn.stylefeng.guns.sys.core.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertUtil {

    public static Date convertToDate(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        Date date1 = new Date(time);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf1.format(date1);

        Date date = null;
        try {
            date = sdf1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 转换数字为decimal
     *
     * @param data 参数
     * @return
     */
    public static BigDecimal convertToDecimal(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return BigDecimal.ZERO;
        }
        BigDecimal decimal = new BigDecimal(data);
        return decimal;
    }

    public static Integer convertBooleanToInt(String flag) {
        if (flag.equals("false")) {
            return 0;
        } else if (flag.equals("true")) {
            return 1;
        }
        return null;
    }

    public static Integer convertToInt(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return 0;
        }
        Integer integer = Integer.valueOf(data);
        return integer;
    }

    public static Long convertToLong(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return 0L;
        }
        Long lon = Long.valueOf(data);
        return lon;
    }

}
